/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Administrator
 */
import module.config.ValueConfig
import module.blog.Link
import module.blog.ArticleClass
class SecurityFilters {
    
    //simple auth 
    def filters = {

        def publicControllers = ['blog','appEngineReload']
        def accountControllers = ['article','articleClass','link','valueConfig','dFile']
        
        def apps = publicControllers+accountControllers
        def loginControllers=accountControllers

        //app check
        appCheck(controller:'*') {
            before = {
                log.debug ' app check @before '

                if(!(controllerName in apps)){
                    log.warn("没有应用 "+controllerName)
                }

                //for first run
                if(!servletContext.isSysInited){
                    //初始化系统配置项
                    //密码和用户名,个人介绍等
                    def user= ValueConfig.findWhere('propertyName':'userName')
                    if(!user){
                        ValueConfig.withTransaction{
                            def v = new ValueConfig(title:'用户名',propertyName:'userName',propertyValue:'管理员')
                            if(!v.save(flush:true)){
                                log.warn '无法初始化用户名'+v.errors
                            }else{
                                log.debug '初始化用户名成功'
                            }
                        }
                        ValueConfig.withTransaction{
                            def v = new ValueConfig(title:'密码',propertyName:'password',propertyValue:'admin')
                            if(!v.save(flush:true)){
                                log.warn '无法初始化密码'+v.errors
                            }else{
                                log.debug '初始化密码成功'
                            }
                        }
                        ValueConfig.withTransaction{
                            def v = new ValueConfig(title:'个人介绍',propertyName:'aboutMe',propertyValue:'IT工作者，Coder<br/>喜欢读书')
                            if(!v.save(flush:true)){
                                log.warn '无法初始化个人介绍'+v.errors
                            }else{
                                log.debug '初始化个人介绍成功'
                            }
                        }

                    }

                    //博客的标题，描述,分类等
                    def title = ValueConfig.findWhere('propertyName':'title')
                    if(!title){
                        ValueConfig.withTransaction{
                            def v = new ValueConfig(title:'博客标题',propertyName:'title',propertyValue:'我的博客')
                            if(!v.save(flush:true)){
                                log.warn '无法初始化博客标题'+v.errors
                            }else{
                                log.debug '初始化博客标题成功'
                            }
                        }
                        ValueConfig.withTransaction{
                            def v = new ValueConfig(title:'博客描述',propertyName:'description',propertyValue:'记录我的生活，感悟，。。。')
                            if(!v.save(flush:true)){
                                log.warn '无法初始化博客描述'+v.errors
                            }else{
                                log.debug '初始化博客描述成功'
                            }
                        }
                        ArticleClass.withTransaction{
                            def c = new ArticleClass(title:'生活',description:'生活点滴')
                            if(c.save(flush:true)){
                                log.warn "无法初始化生活分类"+c.errors
                            }else{
                                log.debug '初始化分类成功'
                            }
                        }
                    }

                    //初始化链接，导航和友情链接
                    def links = Link.list()
                    if(links.size()<1){
                        Link.withTransaction{
                            def l1=new Link(catalog:'导航',title:'首页',link:'/',order:1)
                            if(l1.save(flush:true)){
                                log.debug '初始化导航，首页链接'
                            }else{
                                log.warn '初始化导航，首页失败'
                            }
                        }
                        Link.withTransaction{
                            def l1=new Link(catalog:'导航',title:'管理',link:'/valueConfig',order:2)
                            if(l1.save(flush:true)){
                                log.debug '初始化导航，管理链接'
                            }else{
                                log.warn '初始化导航，管理失败'
                            }
                        }
                        Link.withTransaction{
                            def l1=new Link(catalog:'友情链接',title:'Blog4GG作者',link:'http://sdznyan.appspot.com/',order:1)
                            if(l1.save(flush:true)){
                                log.debug '初始化友情链接，gaeblog作者链接'
                            }else{
                                log.warn '初始化友情链接，gaeblog作者链接失败'
                            }
                        }
                    }

                    //在boot时，初始化异常，在这里初始化系统配置。
                    if(!servletContext.sysConfig){
                        log.info '加载系统配置项'
                        servletContext.sysConfig = new module.config.SysConfigManager().getConfig()
                        log.debug '配置 ：\n'+servletContext.sysConfig
                    }

                    // set isSysInited
                    servletContext.isSysInited = true
                    log.info "初始化系统完毕"
                }
               
            }
        }

        // login check
        loginCheck(controller:'*') {   
            before = {
                if(!session.user) {
                    if(controllerName in loginControllers){
                        log.warn("需要登录，应用 "+controllerName)
                        redirect(controller:'blog',action:'login')
                        return false
                    }
                }
            }
        }
    }
}

